/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Gestión de Capas. 
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package;

import aze.display.TileLayer;
import aze.display.TileSprite;
import aze.display.SparrowTilesheet;
import nme.Assets;
import nme.display.Sprite;
import nme.events.Event;
import nme.ui.Keyboard;
import nme.events.KeyboardEvent;
import nme.Lib;

class LayerExample extends Sprite{

	var _layerBg:TileLayer;           // Capa del Background (Bg)
	var _layerFg:TileLayer;           // Capa del Foreground (Fg)
	var _hashLayer:Hash<TileSprite>;  // Hash de los sprites capa Fg

	// Constructor =====================================================
	private function new() {
		super();

		var sheetData:String = Assets.getText("assets/capas.xml");
		var tilesheet:SparrowTilesheet = new SparrowTilesheet(Assets.getBitmapData("assets/capas.png"), sheetData);
		_layerBg = new TileLayer(tilesheet);  
		_layerFg = new TileLayer(tilesheet);
		_hashLayer = new Hash<TileSprite>();

		addSprite(_layerBg, "Fondo", 100, 100);
		addChild(_layerBg.view);

		addSprite (_layerFg, "CapaA", 150, 150, _hashLayer);
		addSprite (_layerFg, "CapaS", 200, 200, _hashLayer);
		addSprite (_layerFg, "CapaD", 250, 250, _hashLayer);
		addChild(_layerFg.view);

		Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
		Lib.current.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
	}

	// addSprite =======================================================
	function addSprite(layer:TileLayer, sprname:String, x:Int, y:Int,  
		hash:Hash<TileSprite>=null):Void {
		var spr = new TileSprite(sprname);
		spr.x = x; spr.y = y; layer.addChild(spr);
		if (hash != null) hash.set(sprname, spr);  // Aniadimos a Hash
	}

	// Update ==========================================================
	function onEnterFrame(event:Event):Void {
		_layerBg.render();  
		//for (e in _hashLayer) e.x += 1-Std.random(3);
			_layerFg.render();
	}

	// Events ==========================================================
	function onKeyPressed(event:KeyboardEvent):Void {
		var spr : TileSprite = null;
		var keyString : String = null; 
		switch (event.keyCode) {
			case Keyboard.CONTROL: 
				keyString = "A";
			case Keyboard.SPACE:
				keyString = "S";
			case Keyboard.ENTER:
				keyString = "D";
			case Keyboard.ESCAPE:
				subirUltima(_layerFg);
		}
		if (keyString != null) {
			spr = _hashLayer.get("Capa" + keyString);
			_layerFg.removeChild(spr);
			_layerFg.addChildAt(spr, _layerFg.numChildren);
			trace ("Posicion de " + keyString + " : " + _layerFg.indexOf(spr));
			trace (_layerFg.numChildren);
		}
	}

	// Subir capa inferior =============================================
	function subirUltima(layer:TileLayer):Void {
		var ultimo = layer.children.shift();
		var nuevaPos:Int = layer.numChildren-1;
		layer.children.insert(nuevaPos,ultimo);
	}
	
	// Main ============================================================
	public function main() {
		Lib.current.addChild(new LayerExample());
	}
}
